package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.service.goods.CategoryService;
import com.qingcheng.service.goods.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("item")
public class ItemController {

    @Reference
    private SpuService spuService;

    @Autowired
    private TemplateEngine templateEngine;

    @Reference
    private CategoryService categoryService;

    @Value("${pagePath}")
    private String pagePath;

    @GetMapping("/createPage")
    public void createPage(String id){
        Goods goods = spuService.findGoodsById(id);
        Spu spu = goods.getSpu();
        List<Sku> skuList = goods.getSkuList();
        Map<String,String> urlMap=new HashMap<>();
        for (Sku sku : skuList) {

            String specJson=JSON.toJSONString(JSON.parseObject(sku.getSpec()),SerializerFeature.MapSortField);
            urlMap.put(specJson,sku.getId()+".html");
        }
        for (Sku sku : skuList) {
            //1创建上下文
            Context context = new Context();
            //2数据模型
            Map<String,Object> dataTem=new HashMap<>();
            List<String> categoryList = new ArrayList<>();
            categoryList.add(categoryService.findById(spu.getCategory1Id()).getName());
            categoryList.add(categoryService.findById(spu.getCategory2Id()).getName());
            categoryList.add(categoryService.findById(spu.getCategory3Id()).getName());
            dataTem.put("categoryList",categoryList);
            dataTem.put("spu",spu);
            dataTem.put("sku",sku);

            Map paraItems = JSON.parseObject(spu.getParaItems());
            dataTem.put("paraItems",paraItems);
            Map specItems = JSON.parseObject(sku.getSpec());
            dataTem.put("specItems",specItems);

            //{"颜色":["红色","蓝色","紫色","金色","黑色","草色"],"选择内存":["6GB +128GB","6GB+64GB"],"版本":["普通版","星耀版"]}
            //{"选择内存":[{"name":"6GB +128GB","check":true},{"name":"6GB +64GB","check":false}]}
            Map<String,List> specMap=(Map) JSON.parseObject(spu.getSpecItems());
            for (String key : specMap.keySet()) {
                List<Map> mapList = new ArrayList<>();
                List<String> list = specMap.get(key);
                for (String o : list) {
                    Map<String,Object> map=new HashMap<>();
                    if(o.equals(specItems.get(key))){
                        map.put("name",o);
                        map.put("checked",true);
                    }else {
                        map.put("name",o);
                        map.put("checked",false);
                    }
                    Map spec=JSON.parseObject(sku.getSpec());
                    spec.put(key,o);
                    String specJson = JSON.toJSONString(spec,SerializerFeature.MapSortField );
                    map.put("url",urlMap.get(specJson));
                    mapList.add(map);
                }
                specMap.put(key,mapList);
            }
            dataTem.put("specMap",specMap);
            context.setVariables(dataTem);
            //3准备文件
            File file = new File(pagePath);
            if (!file.exists()){
                file.mkdirs();
            }
            File dest=new File(file,sku.getId()+".html");
            //4创建页面
            try {
                PrintWriter printWriter = new PrintWriter(dest,"UTF-8");
                templateEngine.process("item",context,printWriter);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
